package tools;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import lib.Config;
import lib.map.Map;
import client.gui.lobby.SelectorUser;
import client.gui.lobby.mapSelector;

public class MapEditor extends JFrame implements SelectorUser {
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel jButtonPane = null;
	private JPanel mapPanel = null;
	private Map map = null; // @jve:decl-index=0:
	private int Selected = 0; // @jve:decl-index=0:
	private String simple = "";
	ActionListener ComboboxListener = new java.awt.event.ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent e) {
			Selected = jComboBox.getSelectedIndex(); // @jve:decl-index=0:
			simple = "";
		}
	};


	private JTextField SaveTo = null;
	private JButton SaveButton = null;
	private JButton Glider = null;
	private JButton Load = null;
	private mapSelector selector = null;
	private JLabel sep3 = null;
	private JLabel jLabel = null;
	private JButton nextGen = null;
	private JButton GliderButton = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JComboBox jComboBox = null;
	private JLabel jLabel3 = null;
	private JTextField mapSRC = null;
	private JButton ClearMap = null;
	private int xRes=Config.xRes;
	private int yRes=Config.yRes;
	/**
	 * This is the default constructor
	 */
	public MapEditor() {
		super();
		setMap("");
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setState(Frame.MAXIMIZED_BOTH);
		this.setResizable(false);
		this.setSize(xRes, yRes);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setMinimumSize(new Dimension(900, 700));
		this.setContentPane(getJContentPane());
		this.setTitle("MapEditor");
		selector = new mapSelector(this);
		updateCombo();
	}

	/**
	 * This method initializes SaveTo
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getSaveTo() {
		if (SaveTo == null) {
			SaveTo = new JTextField();
			SaveTo.setText("dummy.map");
		}
		return SaveTo;
	}

	void updateCombo() {
		if (jComboBox != null) {
			jComboBox.removeActionListener(ComboboxListener);
			jComboBox.removeAllItems();
			for (int i = 0; i < map.parser.mapList.list.length; i++) {
				if (map.parser.mapList.list[i] != null)
					jComboBox.addItem(map.parser.mapList.list[i].Name);
			}
			jComboBox.setSelectedIndex(0);
			jComboBox.addActionListener(ComboboxListener);
		}
	}

	/**
	 * This method initializes SaveButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getSaveButton() {
		if (SaveButton == null) {
			SaveButton = new JButton();
			SaveButton.setText("Save");
			SaveButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					map.saveMap(SaveTo.getText());
				}
			});
		}
		return SaveButton;
	}

	private void drawGlider(int x, int y) {
		map.setField(1, x, y);
		map.setField(1, x + 1, y + 1);
		map.setField(1, x - 1, y + 2);
		map.setField(1, x, y + 2);
		map.setField(1, x + 1, y + 2);
	}

	private void drawGliderGun(int x, int y) {
		map.setField(1, x, y);
		map.setField(1, x, y + 1);
		map.setField(1, x + 1, y);
		map.setField(1, x + 1, y + 1);
		map.setField(1, x + 8, y + 1);
		map.setField(1, x + 8, y + 2);
		map.setField(1, x + 9, y);
		map.setField(1, x + 9, y + 2);
		map.setField(1, x + 10, y);
		map.setField(1, x + 10, y + 1);
		map.setField(1, x + 16, y + 2);
		map.setField(1, x + 16, y + 3);
		map.setField(1, x + 16, y + 4);
		map.setField(1, x + 17, y + 2);
		map.setField(1, x + 18, y + 3);
		map.setField(1, x + 22, y - 1);
		map.setField(1, x + 22, y);
		map.setField(1, x + 23, y - 2);
		map.setField(1, x + 23, y);
		map.setField(1, x + 24, y - 2);
		map.setField(1, x + 24, y - 1);
		map.setField(1, x + 24, y + 10);
		map.setField(1, x + 24, y + 11);
		map.setField(1, x + 25, y + 10);
		map.setField(1, x + 25, y + 12);
		map.setField(1, x + 26, y + 10);
		map.setField(1, x + 34, y - 2);
		map.setField(1, x + 34, y - 1);
		map.setField(1, x + 35, y - 2);
		map.setField(1, x + 35, y - 1);
		map.setField(1, x + 35, y + 5);
		map.setField(1, x + 35, y + 6);
		map.setField(1, x + 35, y + 7);
		map.setField(1, x + 36, y + 5);
		map.setField(1, x + 37, y + 6);
	}

	/**
	 * This method initializes Glider
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getGlider() {
		if (Glider == null) {
			Glider = new JButton();
			Glider.setName("Glider");
			Glider.setText("Glider Gun");
			Glider.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Selected = 1;
					simple = "GliderGun";
				}
			});
		}
		return Glider;
	}

	/**
	 * This method initializes Load
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getLoad() {
		if (Load == null) {
			Load = new JButton();
			Load.setText("Load");
			Load.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					selector.setVisible(true);
				}
			});
		}
		return Load;
	}

	/**
	 * This method initializes nextGen
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getNextGen() {
		if (nextGen == null) {
			nextGen = new JButton();
			nextGen.setText("Next Generation");
			nextGen.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					map.createParallelUniverseMap();
					repaint();
				}
			});
		}
		return nextGen;
	}

	/**
	 * This method initializes GliderButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getGliderButton() {
		if (GliderButton == null) {
			GliderButton = new JButton();
			GliderButton.setText("Glider");
			GliderButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Selected = 1;
					simple = "Glider";
				}
			});
		}
		return GliderButton;
	}

	/**
	 * This method initializes jComboBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox();
			jComboBox.addActionListener(ComboboxListener);
		}
		return jComboBox;
	}

	/**
	 * This method initializes mapSRC
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getMapSRC() {
		if (mapSRC == null) {
			mapSRC = new JTextField();
			mapSRC.setText("default.msrc");
			mapSRC.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyTyped(java.awt.event.KeyEvent e) {
					if (e.getKeyChar() == '\n') {
						setMap(Config.mapName, mapSRC.getText());
					}
				}
			});
		}
		return mapSRC;
	}

	/**
	 * This method initializes ClearMap
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getClearMap() {
		if (ClearMap == null) {
			ClearMap = new JButton();
			ClearMap.setText("Clear Map");
			ClearMap.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setMap("");
				}
			});
		}
		return ClearMap;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MapEditor thisClass = new MapEditor();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel3 = new JLabel();
			jLabel3.setText("");
			jLabel2 = new JLabel();
			jLabel2.setText("Creatures:");
			jLabel1 = new JLabel();
			jLabel1.setText("");
			jLabel = new JLabel();
			jLabel.setText("Save:");
			sep3 = new JLabel();
			sep3.setText("");
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(20);
			gridLayout.setVgap(11);
			gridLayout.setHgap(0);
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(mapPanel, BorderLayout.WEST);
			jButtonPane = new JPanel();
			jContentPane.add(jButtonPane, BorderLayout.EAST);
			jButtonPane.setLayout(gridLayout);
			jButtonPane.add(getLoad(), null);
			jButtonPane.add(getClearMap(), null);
			jButtonPane.add(getMapSRC(), null);
			jButtonPane.add(jLabel3, null);
			mapPanel.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {

					if (simple.equals("GliderGun")) {
						drawGliderGun(getCollumn(e.getX()), getRow(e.getY()));
						repaint();
					} else if (simple.equals("Glider")) {
						drawGlider(getCollumn(e.getX()), getRow(e.getY()));
						repaint();
					} else {
						setField(e);
						repaint();
					}
				}
			});
			mapPanel
					.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
						public void mouseDragged(java.awt.event.MouseEvent e) {
							setField(e);
							repaint();
						}
					});
			jButtonPane.add(getJComboBox(), null);
			jButtonPane.add(jLabel2, null);
			jButtonPane.add(getGliderButton(), null);
			jButtonPane.add(getGlider(), null);
			jButtonPane.add(sep3, null);
			jButtonPane.add(getNextGen(), null);
			jButtonPane.add(jLabel1, null);
			jButtonPane.add(jLabel, null);
			jButtonPane.add(getSaveTo(), null);
			jButtonPane.add(getSaveButton(), null);
		}
		return jContentPane;
	}

	private void setField(MouseEvent e) {
		map.setField(Selected, getCollumn(e.getX()), getRow(e.getY()));
		repaint();

	}

	public void setMap(String mapName, String mapSrc) {
		setMap(mapName);
		map.setMapSRC(mapSrc);
		repaint();
	}

	public void setMap(String mapName) {
		map = new Map(mapName);

		map.setMapResolution(xRes - 200, yRes - 100);
		System.out.println(Config.mapName);
		mapPanel = new JPanel() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 3839873158685378003L;

			public void paint(Graphics g) {
				g = map.drawMiniMap((Graphics2D) g, 1);
			}
		};
		mapPanel.setLayout(null);
		mapPanel.setPreferredSize(map.getMapResolution());
		mapPanel.setVisible(true);
		updateCombo();
		if (SaveTo != null)
			SaveTo.setText(mapName);
		repaint();

	}

	public int getCollumn(int x) {
		return (int) (x * 1f / (Config.xRes / Config.CollumnCount));
	}

	public int getRow(int y) {
		return (int) (y * 1f / (Config.yRes / Config.RowCount));
	}

}
